 package server.market;

 import client.MapleCharacter;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import handling.channel.ChannelServer;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;






 public class MarketEngine
 {
/*  22 */   private final List<ItemEntry> items = new LinkedList<>();
/*  23 */   private final Map<Integer, String> names = new LinkedHashMap<>();

   public void addItem(int itemId, int quantity, int price, int charid) {
/*  26 */     synchronized (this.items) {
/*  27 */       for (ItemEntry itemEntry : this.items) {
/*  28 */         if (itemEntry.getId() == itemId && itemEntry.getOwner() == charid && itemEntry.getPrice() == price) {
/*  29 */           itemEntry.setQuantity(itemEntry.getQuantity() + quantity);
           return;
         }
       }
     }
/*  34 */     ItemEntry ie = new ItemEntry();
/*  35 */     ie.setId(itemId);
/*  36 */     ie.setQuantity(quantity);
/*  37 */     ie.setOwner(charid);
/*  38 */     ie.setPrice(price);
/*  39 */     synchronized (this.items) {
/*  40 */       this.items.add(ie);
     }
   }

   public void removeItem(int itemId, int quantity, int charid) {
/*  45 */     synchronized (this.items) {
/*  46 */       for (int i = 0; i < this.items.size(); i++) {
/*  47 */         ItemEntry ie = this.items.get(i);
/*  48 */         if (ie.getOwner() == charid && ie.getId() == itemId && ie.getQuantity() >= quantity) {
/*  49 */           if (ie.getQuantity() == quantity) {
/*  50 */             this.items.remove(this.items.indexOf(ie));
           } else {
/*  52 */             ie.setQuantity(ie.getQuantity() - quantity);
           }
         }
       }
     }
   }

   public ItemEntry getItem(int position) {
/*  60 */     return this.items.get(position);
   }

   public List<ItemEntry> getItems() {
/*  64 */     return this.items;
   }

   public String getCharacterName(int charId) {
/*  68 */     if (this.names.get(Integer.valueOf(charId)) != null) {
/*  69 */       return this.names.get(Integer.valueOf(charId));
     }

/*  72 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/*  73 */       for (MapleCharacter mc : cs.getPlayerStorage().getAllCharacters()) {
/*  74 */         if (mc.getId() == charId) {
/*  75 */           this.names.put(Integer.valueOf(charId), mc.getName());
/*  76 */           return mc.getName();
         }
       }
     }
/*  80 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  81 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE id = ?");
/*  82 */       ps.setInt(1, charId);
/*  83 */       ResultSet rs = ps.executeQuery();
/*  84 */       if (rs.next()) {
/*  85 */         String name = rs.getString("name");
/*  86 */         this.names.put(Integer.valueOf(charId), name);
/*  87 */         return name;
       }
/*  89 */     } catch (SQLException e) {
/*  90 */       return "SQL Error fixmepl0x";
     }
/*  92 */     return "No user";
   }


   public String toString() {
/*  97 */     String ret = "";
/*  98 */     synchronized (this.items) {
/*  99 */       for (ItemEntry ie : this.items)
       {

/* 102 */         ret = ret + "#v" + ie.getId() + "# 价格: #b" + ie.getPrice() + "#k卖家: #b" + getCharacterName(ie.getOwner()) + "#k\\r\\n";
       }
     }

/* 106 */     return ret;
   }

   public static class ItemEntry
   {
     private int quantity;
     private int id;
     private int price;
     private int owner;

     public int getId() {
/* 117 */       return this.id;
     }

     public void setId(int id) {
/* 121 */       this.id = id;
     }

     public int getQuantity() {
/* 125 */       return this.quantity;
     }

     public void setQuantity(int quantity) {
/* 129 */       this.quantity = quantity;
     }

     public int getPrice() {
/* 133 */       return this.price;
     }

     public void setPrice(int price) {
/* 137 */       this.price = price;
     }

     public int getOwner() {
/* 141 */       return this.owner;
     }

     public void setOwner(int owner) {
/* 145 */       this.owner = owner;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\market\MarketEngine.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */